<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="学号" prop="stuNo">
        <el-input v-model="queryParams.stuNo" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审核类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择审核类型" clearable>
          <el-option v-for="item in applyType" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
          <el-option v-for="item in dict.type.dms_approval_status" :key="item.value" :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="applicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="学号" align="center" prop="stuNo" />
      <el-table-column label="姓名" align="center" prop="stuName" />
      <el-table-column label="申请表类型" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.type == 1">调宿申请</el-tag>
          <el-tag type="info" v-if="scope.row.type == 2">退宿申请</el-tag>
          <el-tag type="warning" v-if="scope.row.type == 3">留宿申请</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="申请理由" align="center" prop="reason">
        <template slot-scope="scope">
          <el-popover placement="top-start" width="400" trigger="hover" :content="scope.row.reason">
            <p slot="reference" style="display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
              {{ scope.row.reason }}</p>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dms_approval_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == '1'" size="mini" type="text" icon="el-icon-edit"
            @click="handleAudit(scope.row)">审核</el-button>

          <el-button size="mini" type="text" icon="el-icon-view" @click="lookVClick(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改宿舍管理申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form class="audit-form" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学号" prop="stuNo">
          <el-input v-model="form.stuNo" disabled />
        </el-form-item>
        <el-form-item label="姓名" prop="stuName">
          <el-input v-model="form.stuName" disabled />
        </el-form-item>
        <el-form-item label="执行时间" prop="time" v-if="form.type != 3">
          <el-date-picker clearable disabled v-model="form.time" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择执行时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="原宿舍楼" prop="currBuilding">
          <el-input v-model="form.currBuilding" disabled />
        </el-form-item>
        <el-form-item label="原宿舍" prop="currRoomNo">
          <el-input v-model="form.currRoomNo" disabled />
        </el-form-item>
        <el-form-item label="新校区" prop="tarBuilding" v-if="form.type == 1">
          <el-input v-model="this.dormitoryInfo.campusName" disabled />
        </el-form-item>
        <el-form-item label="新园区" prop="tarBuilding" v-if="form.type == 1">
          <el-input v-model="this.dormitoryInfo.parkName" disabled />
        </el-form-item>
        <el-form-item label="新宿舍楼" prop="tarBuilding" v-if="form.type == 1">
          <el-input v-model="this.dormitoryInfo.buildingName" disabled />
        </el-form-item>
        <el-form-item label="新宿舍" prop="targetDormId" v-if="form.type == 1">
          <el-input v-model="this.dormitoryInfo.roomNo" disabled v-if="dormitoryInfo.length != 0" />
          <el-select placeholder="请先选择宿舍楼" v-model="form.targetDormId" v-else :disabled="dormitoryList.length == 0"
            filterable>
            <el-option v-for="item in dormitoryList" :value="item.id" :label="item.roomNo" />
          </el-select>
        </el-form-item>
        <el-form-item label="申请理由" prop="reason">
          <el-input type="textarea" autosize v-model="form.reason" placeholder="请输入申请理由" disabled />
        </el-form-item>
        <el-form-item label="留宿开始时间" prop="startTime" v-if="form.type == 3">
          <el-date-picker clearable disabled v-model="form.startTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择留宿开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="留宿结束时间" prop="endTime" v-if="form.type == 3">
          <el-date-picker clearable disabled v-model="form.endTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择留宿结束时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">同 意</el-button>
        <el-button @click="cancel">拒 绝</el-button>
      </div>
    </el-dialog>


    <el-dialog title="查看" :visible.sync="lookV" width="700px" append-to-body>
      <el-form ref="lookForm" :model="lookForm" label-width="120px">
        <el-form-item label="学号" prop="stuNo">
          <el-input v-model="lookForm.stuNo" disabled />
        </el-form-item>
        <el-form-item label="姓名" prop="stuName">
          <el-input v-model="lookForm.stuName" disabled />
        </el-form-item>
        <el-form-item label="执行时间" prop="time" v-if="lookForm.type != 3">
          <el-date-picker clearable disabled v-model="lookForm.time" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择执行时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="原宿舍楼" prop="currBuilding">
          <el-input v-model="lookForm.currBuilding" disabled />
        </el-form-item>
        <el-form-item label="原宿舍" prop="currRoomNo">
          <el-input v-model="lookForm.currRoomNo" disabled />
        </el-form-item>
        <el-form-item label="新校区" prop="tarBuilding" v-if="lookForm.type == 1">
          <el-input v-model="this.dormitoryInfo.campusName" disabled />
        </el-form-item>
        <el-form-item label="新园区" prop="tarBuilding" v-if="lookForm.type == 1">
          <el-input v-model="this.dormitoryInfo.parkName" disabled />
        </el-form-item>
        <el-form-item label="新宿舍楼" prop="tarBuilding" v-if="lookForm.type == 1">
          <el-input v-model="this.dormitoryInfo.buildingName" disabled />
        </el-form-item>
        <el-form-item label="新宿舍" prop="targetDormId" v-if="lookForm.type == 1">
          <el-input v-model="this.dormitoryInfo.roomNo" disabled />
        </el-form-item>
        <el-form-item label="申请理由" prop="reason">
          <el-input type="textarea" autosize v-model="lookForm.reason" placeholder="请输入申请理由" disabled />
        </el-form-item>
        <el-form-item label="留宿开始时间" prop="startTime" v-if="lookForm.type == 3">
          <el-date-picker clearable disabled v-model="lookForm.startTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择留宿开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="留宿结束时间" prop="endTime" v-if="lookForm.type == 3">
          <el-date-picker clearable disabled v-model="lookForm.endTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择留宿结束时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
    </el-dialog>




  </div>
</template>

<script>
import { listApplication, getApplication, delApplication, addApplication, updateApplication } from "@/api/dormitory/accommodation-management/application";
import { getDormitory } from "@/api/dormitory/basedata/dormitory";
import { fullLoading } from "@/api/helpFunc";

export default {
  name: "Application",
  dicts: ['dms_approval_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 宿舍管理申请表格数据
      applicationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核状态
      status: [{
        value: null,
        label: '全部'
      },
      {
        value: 1,
        label: '已审核'
      },
      {
        value: 0,
        label: '未审核'
      }
      ],
      dormitoryInfo: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuNo: null,
        time: null,
        currentDormId: null,
        targetDormId: null,
        reason: null,
        type: null,
        status: null,
        startTime: null,
        endTime: null,
        getBy: 'work',
        auditStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        targetDormId: [
          { required: true, message: "请选择新宿舍", trigger: "blur" }
        ],
        type: [
          { required: true, message: "申请表类型(1调宿申请不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "申请时间不能为空", trigger: "blur" }
        ],
      },
      // 类型
      applyType: [
        {
          value: null,
          label: '全部'
        },
        {
          value: 2,
          label: '退宿申请'
        },
        {
          value: 1,
          label: '调宿申请'
        }
      ],

      lookForm: {
      },
      lookV: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    lookVClick(row) {
      let id = row.id;
      getApplication(id).then(response => {
        if (response.code == 200) {
          this.lookForm = { ...response.data };
          if (response.data.targetDormId != null) {
            getDormitory(response.data.targetDormId).then(res => {
              this.dormitoryInfo = res.data;
            })
          }
          this.lookV = true;
        }

      });

    },
    /** 查询宿舍管理申请列表 */
    getList() {
      this.loading = true;
      listApplication(this.queryParams).then(response => {
        this.applicationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.form.getBy = 'work';
      this.form.status = -1;
      updateApplication(this.form).then(response => {
        this.form.status = 6;
        this.$modal.msgSuccess("审核成功");
        this.open = false;
        this.getList();
      });
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stuNo: null,
        time: null,
        currentDormId: null,
        targetDormId: null,
        reason: null,
        type: null,
        status: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        startTime: null,
        endTime: null,
        delFlag: null,
        currRoomNo: null,
        currBuilding: null,
        currBuildingId: null,
        tarRoomNo: null,
        tarBuilding: null,
        tarBuildingId: null
      };
      this.buildingList = []
      this.campusList = []
      this.parkList = []
      this.dormitoryList = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.getBy = 'work';
      this.queryParams.auditStatus = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      this.reset();
      const id = row.id || this.ids
      getApplication(id).then(response => {
        this.form = response.data;
        if (response.data.targetDormId != null) {
          getDormitory(response.data.targetDormId).then(res => {
            this.dormitoryInfo = res.data;
          })
        }
        this.open = true;
        if (row.type == 1) {
          this.title = "审核调宿申请表";
        } else if (row.type == 2) {
          this.title = "审核退宿申请表";
        } else if (row.type == 3) {
          this.title = "审核留宿申请表";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.status = 6;
          this.form.getBy = 'work';
          let loading = fullLoading(this);
          updateApplication(this.form).then(res => {
            loading.close();
            if (res.code == 200) {
              this.$modal.msgSuccess("审核成功");
              this.open = false;
              this.getList();
            }

          });
        }
      });
    },
  }
};
</script>
<style scoped lang="scss">
.audit-form {
  .el-form-item {
    margin-bottom: 5px;
  }
}
</style>